package com.etone.smartAudit.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.etone.smartAudit.domain.ServiceExpert;
import com.etone.smartAudit.dto.serviceExpert.ServiceExpertDTO;
import com.etone.smartAudit.error.CustomParameterizedException;
import com.etone.smartAudit.mapper.ServiceExpertMapper;
import com.etone.smartAudit.mapstruct.ServiceExpertMapstruct;
import com.etone.smartAudit.utils.ArraysExtendUtil;
import com.etone.smartAudit.utils.ImageUtil;
import com.etone.smartAudit.vo.FileVO;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.serviceExpert.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author: 蒋学伟
 * @create: 2020-07-22 15:07:26
 * @Description ServiceExpertService
 */
@Service
@AllArgsConstructor
public class ServiceExpertService {
    private ServiceExpertMapper serviceExpertMapper;

    private ServiceExpertMapstruct serviceExpertMapstruct;

    private FileUploadService fileUploadService;

    /**
     * 查询列表
     *
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    public PagerVO<ServiceExpertVO> list(String name, int page, int pageSize) {
        Page<ServiceExpertVO> serviceExpertVOPage = PageHelper.startPage(page, pageSize).doSelectPage(() -> {
            serviceExpertMapper.findList(name);
        });
        return PagerVO.of(serviceExpertVOPage);
    }

    /**
     * 查询基本信息
     *
     * @return
     */
    public List<ServiceExpertBaseVO> listSimple() {
        return serviceExpertMapper.findListSimple();
    }

    /**
     * @param id
     * @return
     */
    public ServiceExpert get(Integer id) {
        ServiceExpert serviceExpert = serviceExpertMapper.selectById(id);
        if (serviceExpert == null) {
            throw new CustomParameterizedException("服务专家不存在");
        }
        return serviceExpert;
    }

    /**
     * 申请次数+1
     *
     * @param ids
     */
    public void addApplyCnt(List<Integer> ids) {
        addApplyCnt(ids, 1);
    }

    /**
     * 申请次数+number
     *
     * @param ids
     */
    public void addApplyCnt(List<Integer> ids, int number) {
        serviceExpertMapper.addApplyCnt(ids, number);
    }


    /**
     * 详情
     *
     * @param id
     * @return
     */
    public ServiceExpertDetailVO detail(Integer id) {
        ServiceExpert serviceExpert = get(id);
        ServiceExpertDetailVO serviceExpertDetailVO = serviceExpertMapstruct.getServiceExpertDetailVO(serviceExpert);

        //支撑任务个数
        ServiceExpertDetailVO supportTaskCnt = serviceExpertMapper.findSupportTaskCnt(id);
        serviceExpertDetailVO.setItemTotal(supportTaskCnt.getItemTotal());
        serviceExpertDetailVO.setItemOngoingTotal(supportTaskCnt.getItemOngoingTotal());
        return serviceExpertDetailVO;
    }

    /**
     * 编辑专家
     *
     * @param id
     * @param serviceExpertDTO
     */
    @Transactional(rollbackFor = Exception.class)
    public void edit(Integer id, ServiceExpertDTO serviceExpertDTO) {
        ServiceExpert serviceExpert = get(id);

        //旧文件
        List<FileVO> oldCover = StringUtils.isEmpty(serviceExpert.getCover()) ? null : Arrays.asList(JSON.parseObject(serviceExpert.getCover(), FileVO.class));
        List<FileVO> oldFiles = serviceExpert.getFiles() == null ? null : JSONArray.parseArray(serviceExpert.getFiles(), FileVO.class);
        List<String> oldDetailFiles = StringUtils.isEmpty(serviceExpert.getDetail()) ? null : ImageUtil.getImgTagUrl(serviceExpert.getDetail());

        BeanUtils.copyProperties(serviceExpertDTO, serviceExpert);
        serviceExpert.setCover(serviceExpertDTO.getCover() == null ? null : JSON.toJSONString(serviceExpertDTO.getCover()));
        serviceExpert.setFiles(CollectionUtils.isEmpty(serviceExpertDTO.getFiles()) ? null : JSONArray.toJSONString(serviceExpertDTO.getFiles()));

        serviceExpertMapper.updateById(serviceExpert);

        //清理图片
        fileUploadService.deleteFileForUpdate(ArraysExtendUtil.mergerArraysList(serviceExpertDTO.getFiles(), Arrays.asList(serviceExpertDTO.getCover())), ArraysExtendUtil.mergerArraysList(oldCover, oldFiles));
        fileUploadService.deleteFileForUpdateByURL(StringUtils.isEmpty(serviceExpertDTO.getDetail()) ? null : ImageUtil.getImgTagUrl(serviceExpertDTO.getDetail()), oldDetailFiles);
    }

    /**
     * @param id
     * @param page
     * @param pageSize
     * @return
     */
    public PagerVO<ServiceExpertMemberVO> memberInfos(Integer id, int page, int pageSize) {
        Page<ServiceExpertMemberVO> serviceExpertMemberVOPage = PageHelper.startPage(page, pageSize).doSelectPage(() -> serviceExpertMapper.findMemberInfos(id));
        return PagerVO.of(serviceExpertMemberVOPage);
    }

    /**
     * 专家支撑项目列表
     *
     * @param id
     * @param page
     * @param pageSize
     * @return
     */
    public PagerVO<ServiceExpertItem> listServiceExpertItem(Integer id, int page, int pageSize) {
        Page<ServiceExpertItem> serviceExpertItemPage = PageHelper.startPage(page, pageSize).doSelectPage(() -> serviceExpertMapper.findListServiceExpertItem(id));
        return PagerVO.of(serviceExpertItemPage);
    }
}
